import datetime
import requests
import pandas as pd
from utils import df_into_db


FREQUENCY_SHIFTING = {
    "1m": 30000,
    "5m": 150000,
    "15m": 450000,
    "30m": 900000,
    "1h": 1800000,
    "8h": 1800000,
    "1d": 14400000,
}
url_table_map =\
{
    "spot": ("https://api.binance.com/api/v1/klines", "spot_k_line"),
    "um": ("https://fapi.binance.com/fapi/v1/klines", "um_k_line"),
    "cm": ("https://dapi.binance.com/dapi/v1/klines", "cm_k_line"),
}

url, table = url_table_map["spot"]
symbol = "BTC/USDT"
end = datetime.datetime(2025, 8, 29, 0, 0, 0, tzinfo=datetime.timezone.utc).timestamp()
start = end - FREQUENCY_SHIFTING["1h"]

params={
    "symbol": symbol,
    "interval": "1h",
    "startTime": int(start*1000),
    "endTime": int(end*1000),
}
response = requests.get(url, params=params)
data = response.json()
column_names = ['start_time', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_asset_volume',
                'num_trades', 'buy_base_asset_volume', 'buy_quote_asset_volume', 'Ignore']
result_df = pd.DataFrame(data, columns=column_names)
result_df["symbol"] = symbol
result_df["frequency"] = "1h"
df_into_db(result_df, table)